# 安卓投屏客户端安装脚本
# 需要以管理员权限运行

param(
    [switch]$SkipDependencies,
    [switch]$Force
)

# 设置控制台编码
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "安卓投屏客户端安装脚本" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# 检查管理员权限
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
    Write-Host "❌ 请以管理员权限运行此脚本" -ForegroundColor Red
    Write-Host "右键点击PowerShell，选择'以管理员身份运行'" -ForegroundColor Yellow
    Read-Host "按回车键退出"
    exit 1
}

# 检查Node.js
Write-Host "正在检查Node.js安装..." -ForegroundColor Yellow
try {
    $nodeVersion = node --version 2>$null
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ Node.js已安装: $nodeVersion" -ForegroundColor Green
    } else {
        throw "Node.js未安装"
    }
} catch {
    Write-Host "❌ 未检测到Node.js，请先安装Node.js" -ForegroundColor Red
    Write-Host "下载地址: https://nodejs.org/" -ForegroundColor Cyan
    Write-Host ""
    Read-Host "按回车键退出"
    exit 1
}

# 检查npm
Write-Host "正在检查npm安装..." -ForegroundColor Yellow
try {
    $npmVersion = npm --version 2>$null
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ npm已安装: $npmVersion" -ForegroundColor Green
    } else {
        throw "npm未安装"
    }
} catch {
    Write-Host "❌ npm未正确安装" -ForegroundColor Red
    Read-Host "按回车键退出"
    exit 1
}

# 安装项目依赖
if (-not $SkipDependencies) {
    Write-Host ""
    Write-Host "正在安装项目依赖..." -ForegroundColor Yellow
    try {
        npm install
        if ($LASTEXITCODE -eq 0) {
            Write-Host "✅ 依赖安装成功" -ForegroundColor Green
        } else {
            throw "依赖安装失败"
        }
    } catch {
        Write-Host "❌ 依赖安装失败" -ForegroundColor Red
        Read-Host "按回车键退出"
        exit 1
    }
}

# 检查scrcpy
Write-Host ""
Write-Host "正在检查scrcpy安装..." -ForegroundColor Yellow
try {
    $scrcpyVersion = scrcpy --version 2>$null
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ scrcpy已安装" -ForegroundColor Green
        Write-Host $scrcpyVersion -ForegroundColor Gray
    } else {
        Write-Host "❌ scrcpy未安装" -ForegroundColor Red
        Write-Host "请下载并安装scrcpy: https://github.com/Genymobile/scrcpy/releases" -ForegroundColor Cyan
    }
} catch {
    Write-Host "❌ scrcpy未安装" -ForegroundColor Red
    Write-Host "请下载并安装scrcpy: https://github.com/Genymobile/scrcpy/releases" -ForegroundColor Cyan
}

# 检查ADB
Write-Host ""
Write-Host "正在检查ADB安装..." -ForegroundColor Yellow
try {
    $adbVersion = adb version 2>$null
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ ADB已安装" -ForegroundColor Green
        $adbVersion | Select-Object -First 1 | ForEach-Object { Write-Host $_ -ForegroundColor Gray }
    } else {
        Write-Host "❌ ADB未安装" -ForegroundColor Red
        Write-Host "请下载并安装Android SDK Platform Tools: https://developer.android.com/studio/releases/platform-tools" -ForegroundColor Cyan
    }
} catch {
    Write-Host "❌ ADB未安装" -ForegroundColor Red
    Write-Host "请下载并安装Android SDK Platform Tools: https://developer.android.com/studio/releases/platform-tools" -ForegroundColor Cyan
}

# 创建启动脚本
Write-Host ""
Write-Host "正在创建启动脚本..." -ForegroundColor Yellow
$startScript = @"
@echo off
chcp 65001 >nul
echo 正在启动安卓投屏客户端...
echo.
echo 请在浏览器中访问: http://localhost:3000
echo 按Ctrl+C停止服务
echo.
npm start
pause
"@

$startScript | Out-File -FilePath "start.bat" -Encoding UTF8
Write-Host "✅ 启动脚本已创建: start.bat" -ForegroundColor Green

# 创建开发模式启动脚本
$devScript = @"
@echo off
chcp 65001 >nul
echo 正在启动安卓投屏客户端（开发模式）...
echo.
echo 请在浏览器中访问: http://localhost:3000
echo 按Ctrl+C停止服务
echo.
npm run dev
pause
"@

$devScript | Out-File -FilePath "start-dev.bat" -Encoding UTF8
Write-Host "✅ 开发模式启动脚本已创建: start-dev.bat" -ForegroundColor Green

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "安装完成！" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "使用方法:" -ForegroundColor Yellow
Write-Host "1. 双击 start.bat 启动服务" -ForegroundColor White
Write-Host "2. 在浏览器中访问: http://localhost:3000" -ForegroundColor White
Write-Host "3. 连接安卓设备并开启USB调试" -ForegroundColor White
Write-Host "4. 点击'刷新设备'查看已连接设备" -ForegroundColor White
Write-Host "5. 点击'开始投屏'开始投屏" -ForegroundColor White
Write-Host ""
Write-Host "开发模式:" -ForegroundColor Yellow
Write-Host "双击 start-dev.bat 启动开发模式（代码修改后自动重启）" -ForegroundColor White
Write-Host ""
Write-Host "注意事项:" -ForegroundColor Yellow
Write-Host "- 确保安卓设备已开启USB调试模式" -ForegroundColor White
Write-Host "- 首次连接需要在设备上授权调试" -ForegroundColor White
Write-Host "- 如果投屏失败，请检查scrcpy和ADB是否正确安装" -ForegroundColor White

Read-Host "按回车键退出" 